package com.cl.unipay4j.wx.transfer;

import com.alibaba.fastjson.JSON;
import com.cl.unipay4j.core.IPayConfig;
import com.cl.unipay4j.core.Result;
import com.cl.unipay4j.core.UnipayConst;
import com.cl.unipay4j.core.transfer.ITransferQuery;
import com.cl.unipay4j.core.transfer.TransferQueryModel;
import com.cl.unipay4j.core.transfer.TransferResponse;
import com.cl.unipay4j.core.transfer.TransferType;
import com.cl.unipay4j.core.util.EnumUtil;
import com.cl.unipay4j.core.util.MoneyUtil;
import com.cl.unipay4j.wx.AbstractWxPay;
import com.cl.unipay4j.wx.sdk.WXPayConstants;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信企业转账查询
 *
 * @Author Small
 * @Date 2020/8/3 14:12
 * @Version 1.0
 **/
@Slf4j
public class WxPayTransferQuery extends AbstractWxPay implements ITransferQuery {
    public WxPayTransferQuery() {
    }

    public WxPayTransferQuery(IPayConfig payConfig) {
        super(payConfig);
    }

    /**
     * 查询企业付款结果
     *
     * @param model
     * @return
     */
    @Override
    public Result execQuery(TransferQueryModel model) {
        log.info("WxTransferQuery::=>订单{} 转账查询请求到达", model.getOutTradeNo());
        try {
            // 查询订单状态
            Map<String, String> map = new HashMap<>();
            map.put("partner_trade_no", model.getOutTradeNo());
            String requestUrl = TransferType.TO_ACCOUNT.equals(model.getTransferType()) ? WXPayConstants.MMPAYMKTTRANSFERS_GETTRANSFERINFO_URL_SUFFIX : WXPayConstants.MMPAYMKTTRANSFERS_QUERY_BANK_URL_SUFFIX;
            Map<String, String> r = initWxPay().transferQuery(map,requestUrl,true);
            log.debug("返回报文：" + JSON.toJSONString(r));
            String returnCode = r.get(WXPayConstants.RETURN_CODE);
            if (UnipayConst.SUCCESS.equals(returnCode)) {
                String resultCode = r.get(WXPayConstants.RESULT_CODE);
                if (UnipayConst.SUCCESS.equals(resultCode)) {
                    return Result.tradeSuccess(wrapperResponse(r));
                } else {
                    String errCode = r.get(WXPayConstants.ERR_CODE);
                    String errCodeDes = r.get(WXPayConstants.ERR_CODE_DES);
                    log.info("WxTransferQuery::=>订单{} 转账查询结果：转账失败,错误代码{},错误原因{}", model.getOutTradeNo(), errCode, errCodeDes);
                    return Result.tradeFail(resultCode,errCode, errCodeDes);
                }
            } else {
                String returnMsg = r.get(WXPayConstants.RETURN_MSG);
                log.info("WxTransferQuery::=>订单{} 转账查询通信失败,错误代码{},错误原因{}", model.getOutTradeNo(), returnCode, returnMsg);
                return Result.returnFail(returnCode, returnMsg);
            }

        } catch (Exception e) {
            log.warn("WxTransferQuery=>订单{} 转账查询发生异常", model.getOutTradeNo(), e);
        }
        return Result.exception();
    }

    private TransferResponse wrapperResponse(Map<String, String> r) {
        WxTransferState stateEnum = EnumUtil.getIEnum(WxTransferState.class, r.get(WXPayConstants.STATUS));
        TransferResponse response = new TransferResponse();
        response.setTransferState(stateEnum.getTransferState());
        response.setOutTradeNo(r.get("partner_trade_no"));
        response.setTradeNo(r.get("payment_no"));
        response.setTotalAmount(MoneyUtil.cent2Yuan(Integer.parseInt(r.get("payment_amount"))));
        response.setBody(JSON.toJSONString(r));
        return response;
    }
}
